#include <gflags/gflags.h>

#include "mysql.hpp"
#include "mysql_chat_session.hpp"
#include "mysql_relation.hpp"
#include "mysql_apply.hpp"

#include "utils.hpp"


DEFINE_bool(run_mode, true, "是否为调试模式");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

class RelationTableTest {
public:
    RelationTableTest(std::shared_ptr<odb::database>& db)
        :_db(db) {}

    void insert() {
        bool ret = false;
        for(int i = 0; i < 5; ++i){
            // std::string uid = std::uuid();
            // std::string fid = std::uuid();
            // ret = _db->insert(uid, fid);
            ret = _db.insert(std::to_string(i), std::to_string(5-i));
            if(!ret) break;
        }
        if(ret){
            std::cout << "成功插入5条数据" << std::endl;
        }else{
            std::cout << "插入数据失败" << std::endl;
        }
    }

    void remove() {
        bool ret = _db.remove("0", "5");
        if(ret){
            std::cout << "删除成功" << std::endl;
        }else{
            std::cout << "删除失败" << std::endl;
        }
    }

    void is_friend() {
        bool ret = _db.is_friend("3", "2");
        if(ret){
            printf("%d-%d是好友\n", 3,2);
        }else{
            printf("%d-%d不是好友\n", 3,2);
        }
    }

    void friends() {
        auto f = _db.friends("1");
        printf("------------- 好友个数：%ld ---------\n", f.size());
        printf("好友ID：---------------------\n");
        for(auto& i : f){
            std::cout << i << std::endl;
        }
    }

    private:
        im::RelationTable _db;
};

class FriendApplyTableTest {
public:
    FriendApplyTableTest(const std::shared_ptr<odb::database>& db)
        :_db(db) {}

    void insert(){
        im::FriendApply fa("32", "12");
        im::FriendApply fa1("22", "12");

        bool ret = _db.insert(fa);
        bool ret1 = _db.insert(fa1);

        if(ret && ret1){
            std::cout << "申请好友成功" << std::endl;
        }else{
            std::cout << "申请好友失败" << std::endl;
        }
    }

    void remove(){
        bool ret = _db.remove("32", "12");

        if(ret){
            std::cout << "移除好友申请成功" << std::endl;
        }else{
            std::cout << "移除好友申请失败" << std::endl;
        }
    }
    
    void exists(){
        bool ret = _db.exists("32", "12");

        if(ret){
            std::cout << "存在好友申请" << std::endl;
        }else{
            std::cout << "不存在好友申请" << std::endl;
        }
    }

    void applyUsers(){
        std::vector<std::string> ret = _db.applyUsers("12");
        std::cout << " 12 的好友申请列表:" << std::endl;
        for(auto it = ret.begin(); it != ret.end(); ++it){
            std::cout << *it << std::endl;
        }
    }

    private:
        im::FriendApplyTable _db;
};

class ChatSessionTableTest {
public:
    ChatSessionTableTest(const std::shared_ptr<odb::database>& db)
        :_db(db) {}

    void insert_test() {
        im::ChatSession cs1("会话ID1", "会话名称1", im::ChatSessionType::SINGLE);
        bool ret = _db.insert(cs1);
        im::ChatSession cs2("会话ID2", "会话名称2", im::ChatSessionType::GROUP);
        bool ret1 = _db.insert(cs2);

        if(ret && ret1){
            std::cout << "会话插入成功" << std::endl;
        }else{
            std::cout << "会话插入失败" << std::endl;
        }
    }


    void select_test() {
        auto res = _db.select("会话ID1");
        std::cout << "---- 会话ID1 ----" << std::endl;
        std::cout << res->chat_session_id() << std::endl;
        std::cout << res->chat_session_name() << std::endl;
        std::cout << (int)res->chat_session_type() << std::endl;
    }

    void single_test() {
        auto res = _db.singleChatSession("b3a6-96f15f76-0000");
        std::cout << "---- b3a6-96f15f76-0000 的单聊会话信息----" << std::endl;
        for (auto &info : res) {
            std::cout << "单聊会话id：" << info.chat_session_id << std::endl;
            std::cout << "会话好友id："<< info.friend_id << std::endl;
        }
    }
    void group_test() {
        auto res = _db.groupChatSession("1d56-513d8e49-0002");
        std::cout << "---- 1d56-513d8e49-0002 的群聊会话信息----" << std::endl;
        for (auto &info : res) {
            std::cout << "群聊会话id：" << info.chat_session_id << std::endl;
            std::cout << "群聊名称：" << info.chat_session_name << std::endl;
        }
    }
    void remove_test() {
        if(_db.remove("会话ID3")){
            std::cout << "删除成功" << std::endl;
        }else{
            std::cout << "删除失败" << std::endl;
        }
    }
    void remove_test2() {
        if(_db.remove("456", "123")){
            std::cout << "删除成功" << std::endl;
        }else{
            std::cout << "删除失败" << std::endl;
        }
    }
    private:
        im::ChatSessionTable _db;
};

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    im::initLogger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    auto db = im::ODBFactory::create("root", "123456", "127.0.0.1", "im", "utf8", 0, 1);

    std::cout << "======================关系表操作=====================" << std::endl;
    // RelationTableTest rt(db);
    // rt.insert();
    // rt.remove();
    // rt.friends();
    // rt.is_friend();

    std::cout << "=====================好友申请表操作==================" << std::endl;
    // FriendApplyTableTest fat(db);
    // fat.insert();
    // fat.applyUsers();
    // fat.exists();
    // fat.remove();

    std::cout << "=======================会话表操作====================" << std::endl;
    ChatSessionTableTest cst(db);
    // cst.insert_test();
    // cst.select_test();
    // cst.single_test();
    // cst.group_test();
    // cst.remove_test();
    // cst.remove_test2();


    return 0;
}